package cn.itcast.nems.oldems.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.itcast.ic.common.bean.Page;
import cn.itcast.ic.common.exception.v2.BizExceptionProducer;
import cn.itcast.ic.common.exception.v2.bean.BizStatusEnum;
import cn.itcast.nems.base.api.ClazzApi;
import cn.itcast.nems.base.dao.dataobject.ClazzDO;
import cn.itcast.nems.base.dao.entity.E2Clazz;
import cn.itcast.nems.base.dao.mapper.ClazzMapper;
import cn.itcast.nems.base.dao.mapper.E2ClazzMapper;
import cn.itcast.nems.base.dto.ClazzDTO;
import cn.itcast.nems.base.dto.ClazzQueryDTO;
import cn.itcast.nems.base.util.ClazzUtil;
import cn.itcast.nems.common.PageQueryModel;
import cn.itcast.nems.common.account.AccountHolder;
import cn.itcast.nems.manager.util.CustomBeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class ClazzServiceImpl implements ClazzApi {

    private final ClazzMapper clazzMapper;

    private final E2ClazzMapper e2ClazzMapper;

    @Override
    public void lock(int clazzId) {
        final E2Clazz e2Clazz = e2ClazzMapper.selectByClazzId(clazzId);
        final Boolean lockFlag = e2Clazz.getLockFlag();
        if (lockFlag != null && lockFlag) {
            BizExceptionProducer.throwBadRequest(BizStatusEnum.BAD_REQUEST.name(), "班级%d已经被锁定", clazzId);
        }
        E2Clazz update = new E2Clazz();
        update.setId(e2Clazz.getId());
        update.setLockFlag(true);
        update.setModifiedBy(AccountHolder.getRequiredAccountInfo().getEmail());
        this.e2ClazzMapper.updateById(update);
    }

    @Override
    public void unlock(int clazzId) {
        final E2Clazz e2Clazz = e2ClazzMapper.selectByClazzId(clazzId);
        final Boolean lockFlag = e2Clazz.getLockFlag();
        if (lockFlag != null && !lockFlag) {
            BizExceptionProducer.throwBadRequest(BizStatusEnum.BAD_REQUEST.name(), "班级%d未锁定", clazzId);
        }
        E2Clazz update = new E2Clazz();
        update.setId(e2Clazz.getId());
        update.setLockFlag(false);
        update.setModifiedBy(AccountHolder.getRequiredAccountInfo().getEmail());
        this.e2ClazzMapper.updateById(update);
    }

    @Override
    public Page<ClazzDTO> queryPageList(@NonNull PageQueryModel<ClazzQueryDTO> queryParams) {
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<ClazzDO> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>();
        page.setCurrent(queryParams.getPageNumber());
        page.setSize(queryParams.getPageSize());
        ClazzDO clazzDO = CustomBeanUtil.copyProperties(queryParams.getCondition(), new ClazzDO());
        if  ((clazzDO.getExcludeFlag() != null && clazzDO.getExcludeFlag())  &&  !AccountHolder.hasEmployeeGroups()){
            return Page.of(queryParams.getPageNumber(), queryParams.getPageSize(), 0, null);
        }
        clazzDO.setEmployeeGroups(AccountHolder.getEmployeeGroups());
        IPage<E2Clazz> result = e2ClazzMapper.queryPageList(page, clazzDO);
        if (CollectionUtil.isEmpty(result.getRecords())) {
            return Page.of(queryParams.getPageNumber(), queryParams.getPageSize(), 0, null);
        }
        return Page.of(
                queryParams.getPageNumber(),
                queryParams.getPageSize(),
                Long.valueOf(result.getTotal()).intValue(),
                result.getRecords().stream().map(ClazzUtil::convert).toList()
        );
    }

    @Override
    public List<?> findClassByParam(ClazzQueryDTO clazzQueryDTO, Integer month, Boolean includeGraduation) {
        ClazzDO clazzDO = new ClazzDO();
        clazzDO.setSchoolId(clazzQueryDTO.getSchoolId());
        clazzDO.setSubjectId(clazzQueryDTO.getSubjectId());
        return clazzMapper.findListBySchoolCodeAndSubjectId(clazzDO);
    }

    @Override
    public List<ClazzDTO> findClazzes(Set<Integer> idSet) {
        if (idSet == null) {
            return null;
        }
        if (CollectionUtil.isEmpty(idSet)) {
            return new ArrayList<>();
        }
        return clazzMapper.findListBySchoolCodeAndSubjectId1(idSet)
                .stream()
                .map(this::doToTdo)
                .collect(Collectors.toList());
    }

    private ClazzDTO doToTdo(ClazzDO clazzDO) {
        ClazzDTO dto = new ClazzDTO();
        dto.setClassId(clazzDO.getClassId());
        dto.setClassName(clazzDO.getClassName());
        dto.setClassTypeId(clazzDO.getClassTypeId());
        dto.setClassTypeName(clazzDO.getClassTypeName());
        dto.setGraduationTime(clazzDO.getGraduationTime());
        dto.setModelId(clazzDO.getModelId());
        dto.setModelName(clazzDO.getModelName());
        dto.setQuantity(clazzDO.getQuantity());
        dto.setSchoolId(clazzDO.getSchoolId());
        dto.setSchoolName(clazzDO.getSchoolName());
        dto.setSort(clazzDO.getSort());
        dto.setStartDate(clazzDO.getStartDate());
        dto.setSubjectId(clazzDO.getSubjectId());
        dto.setSubjectName(clazzDO.getSubjectName());
        dto.setDelayFlag(clazzDO.getDelayFlag());
        dto.setSuspensionFlag(clazzDO.getSuspensionFlag());
        return dto;
    }

    @Override
    public Boolean isLive(Integer clazzId) {
        var e2Clazz = e2ClazzMapper.selectByClazzId(clazzId);
        return e2Clazz != null && "LIVE_BROADCAST".equalsIgnoreCase(e2Clazz.getTeachingModel());
    }
}
